import * as sa from './util'
import ajax from 'axios'
import jsonp from 'jsonp'
import {Message} from 'element-ui'

ajax.defaults.withCredentials = true;



// ajax.interceptors.request.use(config=> {
//   return config;
// }, err=> {
//   console.log({message: '请求超时!'});
//   return Promise.resolve(err);
// })

// ajax.interceptors.response.use(data=> {
  // if (data.status && data.status == 200 && data.data.status == 'error') {
  //   console.log({message: data.data.msg});
  //   return;
  // }
  // conosle.log(data.data)
  // if(data.data.code == 401){
  //   Message.warning('登录信息失效，请重新登录')
  //   saLogout()
  // }
  // return data;
// }, err=> {
  // if (err.response.status == 504||err.response.status == 404) {
  //   Message.warning({message: '服务器被吃了⊙﹏⊙∥'});
  // } else if (err.response.status == 403) {
  //   Message.warning({message: '权限不足,请联系管理员!'});
  // }else if(err.response.data.code === 500){
  //   Message.warning(err.response.data.msg);
  // }else if(false)
  //   return Promise.resolve(err);

  // console.log(err.response.data);
  // if(err.response.data.code == 401){
  //   Message.warning('登录信息失效，请重新登录')
  //   saLogout()
  // }
// })

///处理请求头
//同时接收一个提示回调
function disposeJsonHeader(json,tip){
  var result = false,
      show = tip && tip.warning || function(){};

  if(json && json.code === 0){
    if(json.data){
      if(!Object.keys(json.data).length) {
        show('没有获取到相关数据');
      }else{
        result = json.data;

      }

      //没有data 则直接返回json
    }else{
      result = json;
    }

  }else if(json.code == 500){
    show(json.msg);

  }else if(json.code == 401 || json.code == 403){
    console.log('401')
    show('登录信息失效，请重新登录');
    saLogout();

    //显示错误
  }else{
    show(json.msg);
  }

  return result;
}

//普通的get请求
function normalGet(url,params,success,error,tip,always){
  let suc = success || function(){},
      err = error || function(){},
      al = always || function(){};

  ajax.get(url,{params:params}).then((res) =>{

    if(res.data){
      try{
        let data = disposeJsonHeader(res.data,tip);
        if(data) suc(data);
      }catch (e){
        console.warn(e);
      }
    }
    al();
  }).catch((e) =>{
    al();
    err();
    console.log('err ')
  })
}

//普通post请求
function normalPost(url,params,success,error,tip,always){
  let suc = success || function(){},
      err = error || function(){},
      al = always || function(){};

  ajax.post(url,params).then((res) =>{

    if(res.data){
      try{
        let data = disposeJsonHeader(res.data,tip);
        if(data) suc(data);
      }catch (e){
        console.warn(e);
      }
    }
    al();
  }).catch((e) =>{
    console.log('catch err')
    al();
    err();
    console.log('post err')
  })
}

//设置token
function saSetToken(t) {
  let token = '';
  if (t) {
    token = t;
    sa.localSave('yunlifangToken', token);
  } else {
    token = sa.localSave('yunlifangToken');
  }

  ajax.defaults.headers.token = token;
  return token;
}


//获取当前用户信息
let saGetUserInfo = function () {
  return sa.localSave('yunlifangUserInfo') || false;
}

//退出登录
function saLogout() {
  sa.localSave('yunlifangUserInfo', '');
  sa.localSave('yunlifangToken', '');
  console.log("logout");
  location.href = '#/login';
}

export  {
  normalGet,
  normalPost,
  saSetToken,
  saGetUserInfo,
  saLogout,
}
